<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<title>菜鸟hh</title>
</head>

<body onload="checkCookie()">



<script>

function setCookie(cname, v) {
    var d = new Date()
    d.setTime(d.getTime() + 86400)
    var r = cname + "=" + v + "; expires="+d.toGMTString()
    document.write("set Cookie: "+r+"<br>")
    document.cookie = r
}
function getCookie(cname) {
    document.write("get Cookie: ", cname+"<br>")

    var name = cname + "="
    var ca = document.cookie.split(";")
    document.write("get Cookie ca: ", ca+"<br>")

    for (var i=0; i < ca.length; i++) {
        var c = ca[i].trim();
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length)
        }
    }
    return ""
}
function checkCookie() {
    document.write("--------check Cookie------------------<br>")

    name = getCookie("username")
    document.write("check Cookie get:", name+"<br>")
    if (name != null && name != "") {
        alert("欢迎:" + name)
    } else {
        var r = prompt("输入：", "")
        if (r == null || r == "") {
            alert("输入非法！！！")
        } else {
            setCookie("username", r)
        }
    }
}

</script>

</body>
</html>

